<template>
    <ContentBase>
        <div class="row justify-content-md-center">
            <div class="col-3">
                <form @submit.prevent="register">
                    <div class="mb-3">
                        <label for="username" class="form-label">用户名</label>
                        <input v-model="username" type="text" class="form-control" id="username">
                    </div>
                    <div class="mb-3">
                        <label for="password" class="form-label">密码</label>
                        <input v-model="password" type="password" class="form-control" id="password">
                    </div>
                    <div class="mb-3">
                        <label for="password_confirm" class="form-label">确认密码</label>
                        <input v-model="password_confirm" type="password" class="form-control" id="password_confirm">
                    </div>
                    <select class="form-select select-identity" aria-label="Default select example" v-model="identity">
                        <option selected>选择身份</option>
                        <option value="student">学生</option>
                        <option value="teacher">老师</option>
                    </select>
                    <div class="error-message">{{ error_message }}</div>
                    <button type="submit" class="btn btn-primary">注册</button>
                </form>
            </div>
        </div>
    </ContentBase>
</template>

<script setup>
import ContentBase from '../components/ContentBase';
import { ref } from 'vue';
import { useStore } from 'vuex';
import router from '@/router/index';
import $ from 'jquery';
import { onMounted } from 'vue';

const store = useStore();
let username = ref('');
let password = ref('');
let password_confirm = ref('');
let identity = ref('选择身份');
let error_message = ref('');

onMounted(() => {
    store.commit("updatePullingInfo", false);
})

const register = () => {
    error_message.value = "";
    $.ajax({
        url: "http://localhost:3000/api/account/register",
        type: "POST",
        data: {
            username: username.value,
            password: password.value,
            confirmedPassword: password_confirm.value,
            identity: identity.value
        },
        success(resp) {
            if (resp.error_message === "success") {
                store.dispatch("login", {
                    username: username.value,
                    password: password.value,
                    success() {
                        if (store.state.user.identity == 'teacher') {
                            router.push({name: 'TeacherInvoice'});
                        } else if (store.state.user.identity == 'student') {
                            router.push({name: 'InvoiceRecord'});    // push的内容跟Navbar中跳转的内容一样
                        }
                    },
                    error() {
                        error_message.value = "系统异常，请稍后重试";
                    }
                });
            } else {
                error_message.value = resp.error_message;
            }
        }
    })
};

</script>

<style scoped>

button {
    width: 100%;
}

.error-message {
    color: red;
}

.select-identity {
    margin: 20px 0;
}

</style>